package com.project.lk.nurseapp.nurse.doctorapply;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.base.UrlContants;
import com.project.lk.nurseapp.common.adapter.AllTagItemAdapter;
import com.project.lk.nurseapp.nurse.doctorapply.model.DoctorTypeBean;
import com.project.lk.nurseapp.nurse.doctorapply.presenter.DoctorListPresenter;
import com.project.lk.nurseapp.nurse.doctorapply.presenter.IDoctorListView;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.XViewSettingUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class DoctorListActivity extends BaseActivity implements IDoctorListView {


    @BindView(R.id.flowtagRecycleview)
    RecyclerView flowtagRecycleview;
    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    private DoctorListPresenter presenter;
    private List<DoctorTypeBean> typeList = new ArrayList<>();
    private String departMentId = "";
    private DoctorListAdapter adapter;
    private AllTagItemAdapter tagAdapter;

    @Override
    protected void initData() {

        presenter = new DoctorListPresenter(this);
        presenter.loadTypeData(UrlContants.DEPART_TYPE_URL);
        presenter.loadListData(UrlContants.DOCTOR_LIST_URL,departMentId);
    }

    /**
     * 初始化数据
     */
    List<String> dataSource = new ArrayList<>();



    @Override
    protected void initView() {

        LinearLayoutManager manager=new LinearLayoutManager(this);
        adapter = new DoctorListAdapter(presenter.getItemBeanList(),this);
         XViewSettingUtils.setting(xrecyclerview,adapter,manager);
        xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.refresh(UrlContants.DOCTOR_LIST_URL,departMentId);
            }

            @Override
            public void onLoadMore() {
                presenter.loadMore(UrlContants.DOCTOR_LIST_URL,departMentId);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_doctor_list;
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        AppManager.getAppManager().finishActivity();
    }

    @Override
    public void typeListSuccess() {
        if (presenter != null) {
            typeList = presenter.getTypeList();

            for (int i = 0; i < typeList.size(); i++) {
                String departmentName = typeList.get(i).getDepartmentName();
                dataSource.add(departmentName);
            }

            tagAdapter = new AllTagItemAdapter(dataSource,this);
            StaggeredGridLayoutManager gridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL);
            flowtagRecycleview.setAdapter(tagAdapter);
            flowtagRecycleview.setLayoutManager(gridLayoutManager);

            tagAdapter.setItemClick(new AllTagItemAdapter.OnItemClickListener() {
                @Override
                public void setOnItemClickListener(int position, View view) {
                    departMentId = typeList.get(position).getId();
                    presenter.loadListData(UrlContants.DOCTOR_LIST_URL,departMentId);
                    ToastUtils.showShortToast(dataSource.get(position));
                }
            });
        }
    }

    @Override
    public void typeFail() {

    }

    @Override
    public void showList() {
        if (adapter!=null) {
            adapter.notifyDataSetChanged();
            xrecyclerview.refreshComplete();
            xrecyclerview.loadMoreComplete();
        }
    }

    @Override
    public void showFail() {
        if (xrecyclerview!=null) {
            xrecyclerview.refreshComplete();
            xrecyclerview.loadMoreComplete();
        }
    }

    @Override
    public void noMore() {
        if (xrecyclerview!=null) {
            xrecyclerview.refreshComplete();
            xrecyclerview.loadMoreComplete();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.dettachView();
            presenter = null;
        }
    }



}
